@echo off

cls

rem -----------------------------------------------------------------------------

set MODE=%~1
set ADDRESSMODEL=%~2
set ACTION=%~3

set SOURCE_DIRECTORY=%~4
REM set SOURCE_DIRECTORY=%SOURCE_DIRECTORY:~0,-1%

set GENERATOR=%~5
set TOOLSET=%~6
set COMPILER_TAG=%~7

rem -----------------------------------------------------------------------------

if "%SOURCE_DIRECTORY%" == "" (
    echo ERROR: cource directory not set
    exit /b
)  

set SOURCE_DIRECTORY_PRIVATE=%SOURCE_DIRECTORY:~0,-1%
if "%NAME_PRODUCT%"     == "" ( 
    for %%f in (%SOURCE_DIRECTORY_PRIVATE%) do set NAME_PRODUCT=%%~nxf
)  

if "%GENERATOR%"        == ""   set GENERATOR=MinGW Makefiles
if "%MODE%"             == ""   set MODE=Debug
if "%ADDRESSMODEL%"     == ""   set ADDRESSMODEL=32
if "%ACTION%"           == ""   set ACTION=build
if "%BUILDDIR%"         == ""   set BUILDDIR=%MODE%%ADDRESSMODEL%
if "%COMPILER_TAG%"     == ""   set COMPILER_TAG=mingw

echo PROJECT NAME ......... %NAME_PRODUCT%
echo SOURCE DIRECTORY ..... %SOURCE_DIRECTORY%
echo IDE .................. %GENERATOR%%SUFFIX%
echo BUILD TYPE ........... %MODE%
echo ADDRESS MODEL ........ %ADDRESSMODEL%
echo ACTION ............... %ACTION%

rem -----------------------------------------------------------------------------

if not exist build ( mkdir build )
cd build

set _build_dir=%COMPILER_TAG%-%MODE%-%ADDRESSMODEL%-%NAME_PRODUCT%
if not exist %_build_dir% ( mkdir %_build_dir% )
cd %_build_dir%


rem -----------------------------------------------------------------------------

set WHERE_ARE_LAUNCHED_THIS_BAT=%~dp0
set MINGW_LOCATION=%WHERE_ARE_LAUNCHED_THIS_BAT%..\mingw%ADDRESSMODEL%\bin

set PATH=%MINGW_LOCATION%;%PATH%

rem -----------------------------------------------------------------------------

cmake  -G "%GENERATOR%" -DCMAKE_BUILD_TYPE=%MODE%  %SOURCE_DIRECTORY%

rem -----------------------------------------------------------------------------

echo [ %ACTION% ] ...

if "%ACTION%" == "stop" exit/b

if "%ACTION%" == "launch" (
    start qtcreator.exe %SOURCE_DIRECTORY%CMakeLists.txt
) ELSE (
    if "%ACTION%" == "build" mingw32-make
)

rem -----------------------------------------------------------------------------

cd ..
cd ..

exit/b
       

